package com.itboye.sunsun.person.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.sunsun.luntan.fragment.WoDePingLunFragment;
import com.itboye.sunsun.luntan.fragment.WoDeZhuTiFragment;
import com.itboye.sunsun.me.ui.KeFuMeFragment;
import com.itboye.sunsun.me.ui.MyTieziFragment;
import com.itboye.sunsun.me.ui.MyTieziHuifuFragment;
import com.itboye.sunsun.product.center.KeFuFragment;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsunhome.R;
/**
 * 
 * @author Mr.liu
 * @author 帖子收藏
 *
 */
public class MyCollActivity extends BaseActivity implements OnClickListener {

	LinearLayout layoutMycollTiezi;
	LinearLayout layoutTieZiHuiFu;

	LinearLayout content_layoutMycoll;
	
	TextView add_shap_title_tv;

	// 头部标签切换的Fragment
	private Fragment WodehuifuFragment, MyTieziHuifu, currentFragment;
	Button btnTiezi, btnTieziHuifu;

	private View viewright, viewLeift;
	
	ImageView close_icon;
	
	@Override
	@SuppressLint("InlinedApi")
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		
		setContentView(R.layout.activity_mycoll);

		initTab();
		setStatusBarColor(R.color.home_blue);
		add_shap_title_tv.setText("我的帖子");
		close_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	/**
	 * 初始化头部标签
	 */
	@SuppressWarnings("deprecation")
	private void initTab() {
		if (WodehuifuFragment == null) {
			WodehuifuFragment = new MyTieziFragment();
		}

		if (!WodehuifuFragment.isAdded()) {
			// 提交事务
			getSupportFragmentManager().beginTransaction()
					.add(R.id.content_layoutMycoll, WodehuifuFragment).commit();

			// 记录当前Fragment
			currentFragment = WodehuifuFragment;
			// 设置图片文本的变化
			btnTiezi.setTextColor(getResources().getColor(R.color.home_blue));
			btnTieziHuifu.setTextColor(getResources().getColor(R.color.gray));
			viewright.setBackgroundColor(getResources().getColor(
					R.color.home_blue));// (R.color.home_blue);
			viewLeift.setBackgroundColor(getResources().getColor(
					R.color.backgroundColor));
		}
	}

	private void clickTab1Layout() {
		if (WodehuifuFragment == null) {
			WodehuifuFragment = new MyTieziFragment();
		}
		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				WodehuifuFragment);

		// 设置底部tab变化
		btnTiezi.setTextColor(getResources().getColor(R.color.home_blue));
		btnTieziHuifu.setTextColor(getResources().getColor(R.color.gray));
		viewright
				.setBackgroundColor(getResources().getColor(R.color.home_blue));// (R.color.home_blue);
		viewLeift.setBackgroundColor(getResources().getColor(
				R.color.backgroundColor));
	}

	/**
	 * 点击第二个tab
	 */
	private void clickTab2Layout() {
		if (MyTieziHuifu == null) {
			MyTieziHuifu = new MyTieziHuifuFragment();
		}
		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				MyTieziHuifu);

		btnTieziHuifu.setTextColor(getResources().getColor(R.color.home_blue));
		btnTiezi.setTextColor(getResources().getColor(R.color.gray));
		viewright.setBackgroundColor(getResources().getColor(
				R.color.backgroundColor));// (R.color.home_blue);
		viewLeift
				.setBackgroundColor(getResources().getColor(R.color.home_blue));
	}

	/**
	 * 添加或者显示碎片
	 * 
	 * @param transaction
	 * @param fragment
	 */
	private void addOrShowFragment(FragmentTransaction transaction,
			Fragment fragment) {
		if (currentFragment == fragment)
			return;

		if (!fragment.isAdded()) { // 如果当前fragment未被添加，则添加到Fragment管理器中
			transaction.hide(currentFragment)
					.add(R.id.content_layoutMycoll, fragment).commit();
		} else {
			transaction.hide(currentFragment).show(fragment).commit();
		}

		currentFragment = fragment;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layoutMycollTiezi:
			clickTab1Layout();
			break;
		case R.id.layoutTieZiHuiFu:
			clickTab2Layout();
			break;
		case R.id.btnTiezi:
			clickTab1Layout();
			break;
		case R.id.btnTieziHuifu:
			clickTab2Layout();
			break;
		default:
			break;
		}
	}
}
